////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  Filename:    ZombiesVsCheerleadGuts.cpp											  //
//  Developer:																  //	
//  Create date:                                                              //
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//                                                                            //
//  Description: This is the class implementation for the slot gut of the     //
//               Australian Slot machine called El Luchador.			      //
//					(client provided by Genesis)							  //	
//                                                                            //
////////////////////////////////////////////////////////////////////////////////
//        2010 - Riplife Gaming.  All rights reserved.						  //
////////////////////////////////////////////////////////////////////////////////

#include "ZombiesVsCheerleadGuts.h"
#include "slotconn.h"
#include "sltsql.h"


ZombiesVsCheerleadGuts::SpinResult ZombiesVsCheerleadGuts::m_aLineWinSpinResults[][6] = 
{
// Number of icons:			0			1						2					3					4					5
/* ICON_WILD */			{	EVT_LOSER,	EVT_LOSER,				EVT_TWO_WILD,		EVT_THREE_WILD,		EVT_FOUR_WILD,		EVT_FIVE_WILD		},
/* ICON_PIC1 */			{	EVT_LOSER,	EVT_LOSER,				EVT_TWO_PIC1,		EVT_THREE_PIC1,		EVT_FOUR_PIC1,		EVT_FIVE_PIC1		},
/* ICON_PIC2 */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_THREE_PIC2,		EVT_FOUR_PIC2,		EVT_FIVE_PIC2		},
/* ICON_PIC3 */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_THREE_PIC3,		EVT_FOUR_PIC3,		EVT_FIVE_PIC3		},
/* ICON_PIC4 */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_THREE_PIC4,		EVT_FOUR_PIC4,		EVT_FIVE_PIC4		},
/* ICON_ACE */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_THREE_ACE,		EVT_FOUR_ACE,		EVT_FIVE_ACE		},
/* ICON_KING */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_THREE_KING,		EVT_FOUR_KING,		EVT_FIVE_KING		},
/* ICON_QUEEN */		{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_THREE_QUEEN,	EVT_FOUR_QUEEN,		EVT_FIVE_QUEEN		},
/* ICON_JACK */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_THREE_JACK,		EVT_FOUR_JACK,		EVT_FIVE_JACK		},
/* ICON_TEN */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_THREE_TEN,		EVT_FOUR_TEN,		EVT_FIVE_TEN		},
/* ICON_NINE */			{	EVT_LOSER,	EVT_LOSER,				EVT_TWO_NINE,		EVT_THREE_NINE,		EVT_FOUR_NINE,		EVT_FIVE_NINE		},
/* ICON_SCATTER */		{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			}
};


////////////////////////////////////////////////////////////////////////////
/// ZombiesVsCheerleadGuts::m_aScatterWinSpinResults
///
/// This array is used to quickly get the SpinResult for the number of
/// icons.  It is indexed by wheel icon and then count.  So to get the
/// SpinResult for 5 Scatter, use the following code snippet:
///
///		SpinResult result = m_aLineWinSpinResults[ ICON_SCATTER ][ 5 ];
///
/// In this example result would be set to EVT_FIVE_SCATTER.
////////////////////////////////////////////////////////////////////////////
ZombiesVsCheerleadGuts::SpinResult ZombiesVsCheerleadGuts::m_aScatterWinSpinResults[][6] = 
{
// Number of icons:			0			1						2					3					4					5
/* ICON_WILD */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_PIC1 */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_PIC2 */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_PIC3 */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_PIC4 */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_ACE */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_KING */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_QUEEN */		{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_JACK */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_TEN */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_NINE */			{	EVT_LOSER,	EVT_LOSER,				EVT_LOSER,			EVT_LOSER,			EVT_LOSER,			EVT_LOSER			},
/* ICON_SCATTER */		{	EVT_LOSER,	EVT_SCATTER_NEAR_MISS,	EVT_TWO_SCATTER,	EVT_THREE_SCATTER,	EVT_FOUR_SCATTER,	EVT_FIVE_SCATTER	}
};


////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::ZombiesVsCheerleadGuts()   
///
/// This is the default constructor for the class.
////////////////////////////////////////////////////////////////////////////
ZombiesVsCheerleadGuts::ZombiesVsCheerleadGuts()
{
	// If this doesn't compile it means that someone probably added
	// another icon type but did not update the
	// m_aScatterWinSpinResults array. 
	C_ASSERT( countof( m_aScatterWinSpinResults ) == NUM_VALID_ICONS );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::~ZombiesVsCheerleadGuts()
///
/// Destructor for ZombiesVsCheerleadGuts. 
////////////////////////////////////////////////////////////////////////////
ZombiesVsCheerleadGuts::~ZombiesVsCheerleadGuts()
{
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::getScatterWinResult()   
///
/// @param   spin				The current spin object.
/// @param   testIcon			The scatter icon that we are searching for.
/// @param   scatterPositions	The bit mask for the scatters positions that we find.
///
/// @return	 SpinResult			A payout from the SpinResult enumeration.
///
/// This helper method is used to evaluate the spin for scatters.
////////////////////////////////////////////////////////////////////////////
ZombiesVsCheerleadGuts::SpinResult ZombiesVsCheerleadGuts::getScatterWinResult( SlotSpin* spin, uint8 testIcon, int32& scatterPositions ) const
{
	// Declare a local variable to keep up with the icon count.
	uint8 numScatterIcons = 0;

	// Initialize the scatterPositions.
	scatterPositions = 0;

	// Loop through each reel.
  	for( int reel = 0; reel < getReelCount(); reel++ )
	{
		// The position bit for the current icon in case it needs to be stored in a mask.
		uint8 position = reel;

		// Loop through each row in the wheel house.
		for( int row = 0; row < 3; row++ )
		{
			// Get the current stop.
			uint8 stop = relativeWheelPosition( getGameModeId(), reel,
				spin->getStops()[ reel ], row );

			// Get the icon for the current stop.
			uint8 currentIcon = getIcon( getGameModeId(), reel, stop )->iconid;
			
			// If the icon is the one we are looking for...
			if ( currentIcon == testIcon )
			{
				// then increase the icon count
				numScatterIcons++;

				// Set the bit in the position mask for this stop.
				scatterPositions |= 1 << position;
			}

			// Move to the next bit in the position mask.
            position += 5;
		}
	}

	// Use the array to look up the spin result for numScatterIcons.
	return lookupScatterWinSpinResult( testIcon, numScatterIcons );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::evaluateScatterWin()
///
/// This function determines if the current spin wins a scatter payment and
/// adds the scatter.
////////////////////////////////////////////////////////////////////////////
void ZombiesVsCheerleadGuts::evaluateScatterWin( SlotSpin* spin, SpinResult srScatterResult, int32 scatterPositions )
{
	// If we got the scatter win (or almost got it)
	if ( srScatterResult != EVT_LOSER )
	{
		// Get the base payout for the scatter.
		int32 nPayout = getPayout( srScatterResult )->payoutAmount;

		// Add the payout (with multiplier and bet per line) to the total win for this spin.
		increaseTotalWin( nPayout * getBetPerLine() * getNumLinesBet() );
    
		// If we have more than three scatters, free spins awarded
		int nFreeSpinsAwarded = 0;
		if (srScatterResult == EVT_THREE_SCATTER ||  srScatterResult == EVT_FOUR_SCATTER || srScatterResult == EVT_FIVE_SCATTER)
		{
			nFreeSpinsAwarded = 6;
		}

		// Add a scatter to the spin
		spin->addScatter( scatterPositions, nPayout * getBetPerLine() * getNumLinesBet(), 
			              1, false, srScatterResult, nFreeSpinsAwarded );
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::classifyZombiesVsCheerleadScatters( SlotSpin* spin, uint8 FeatMultiplier )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This is an override of the framework method.  
/// It determines if the current spin merits free spins or scatter payments.
///
/// Returns: scatter positions (binary mask)
////////////////////////////////////////////////////////////////////////////
ZombiesVsCheerleadGuts::SpinResult ZombiesVsCheerleadGuts::classifyZombiesVsCheerleadScatters( SlotSpin* spin )
{	
	// Try for the Random Progressive Jackpot
	tryForJackpot( spin );

	// Declare a local variable to track the scatter positions.
	int32 nScatterPositions = 0;

	// See if there is a scatter.
	SpinResult srResult = getScatterWinResult( spin, ICON_SCATTER, nScatterPositions );

	// Add the scatter, if any.
	evaluateScatterWin( spin, srResult, nScatterPositions );

	return srResult;
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::isValidGuts()   
///
/// @return	 bool8      True if gut is valid.    
///
/// This helper method is used to see if the current gut ID is valid.
////////////////////////////////////////////////////////////////////////////
bool8 ZombiesVsCheerleadGuts::isValidGuts()
{
	// Make sure that the m_aScatterWinSpinResults array has an entry for each reel,
	// and an additional entry in case there are 0 icons on the line.
	ASSERT( countof( m_aScatterWinSpinResults[0] ) == getReelCount( MODE_NORMAL ) + 1 );

	// Now make sure that both game modes have the same number of reels.
	ASSERT( getReelCount( MODE_NORMAL ) == getReelCount( MODE_FREE_SPIN ) );

	// Finally just check to see if the gut is within the right range. GENESIS GAMES: [9000,9500)
    return ( getGutsId() >= 9000 && getGutsId() < 9500 );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::getGameModeMultiplierAndNoteId( uint8& nGameModeMultiplier, uint8& nNoteId )
///
/// @param   nGameModeMultiplier	The multiplier for the game mode.
/// @param   nNoteId				The note ID for the game mode.
///
/// This helper method is used to get the game mode multiplier and note ID.
////////////////////////////////////////////////////////////////////////////
void ZombiesVsCheerleadGuts::getGameModeMultiplierAndNoteId( uint8& nGameModeMultiplier,
	uint8& nNoteId )
{
	// If we are in free spin mode...
    if ( getGameModeId() == MODE_FREE_SPIN )
    {
		
    }
	// Otherwise...
    else
    {
		// just defer to the base class.
        SlotGuts::getGameModeMultiplierAndNoteId( nGameModeMultiplier, nNoteId );
    }
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::preSpinClassifyResults( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This method is overridden when running in debug spins mode to alter the reel stops.
////////////////////////////////////////////////////////////////////////////
void ZombiesVsCheerleadGuts::preSpinClassifyResults( SlotSpin* spin )
{
	SlotGuts::preSpinClassifyResults(spin);
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::postSpinClassifyResults( SlotSpin* spin )
///
/// @param   spin       Pointer to the spin with stop data.
///
/// This method is overridden to see if we can complete the game.
////////////////////////////////////////////////////////////////////////////
void ZombiesVsCheerleadGuts::postSpinClassifyResults( SlotSpin* spin )
{
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::hasSavedGames()
///
/// @param   bool8   True/False indicating if this machine supports saved games.
///
/// This is a framework override to determine if this machine supports saved games.
////////////////////////////////////////////////////////////////////////////
bool8 ZombiesVsCheerleadGuts::hasSavedGames()
{
	// We support saved games so always return true.
	return true;
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::Debug_SetLoserStops( uint8* pStops )
///
/// @param   pStops		The stops to set.
///
/// Base class override, returns a losing spin triggered by the debug tool
////////////////////////////////////////////////////////////////////////////
void ZombiesVsCheerleadGuts::Debug_SetLoserStops( uint8* pStops )
{
	pStops[0]=0;
	pStops[1]=0;
	pStops[2]=0;
	pStops[3]=0;
	pStops[4]=0;
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::getProgressiveValue( )
///
/// Returns current jackpot value for ZombiesAndCheerleaders machine
////////////////////////////////////////////////////////////////////////////
int32 ZombiesVsCheerleadGuts::getProgressiveValue()
{
   SlotsSQL *slotTracker = (SlotsSQL *) tracker;
   int32 jackpot = 0;

    if ( tracker )
    {
    	slotTracker->lock();
		slotTracker->getJackpot(getJackpotId(), jackpot, getInfo()->forMoney, 1);
    	slotTracker->unlock();
    }

	return jackpot;
}

void ZombiesVsCheerleadGuts::classifyResult(SlotSpin *spin)
{
	uint8 *classifyPositions = new uint8[spin->getStopCount()];
    uint8 result = 0;

	// First of all, before evaluating lines, classify scatters
	// Classify any scatter pays (implemented as a null function in the base)
	spin->clearScatters();
	SpinResult scatterResult = classifyZombiesVsCheerleadScatters(spin);

	// We can have up to two prizes per line in Zombies Vs Cheerleaders (Left to Right + Right to Left), so we are 
	// going to create new lines in order to support this.
	// Lines 1-25	-> Left to Right wins
	// Lines 26-50	-> Right to Left wins
	// We have just created lines from 1 to 25, so we have to create the rest of them:
	for ( uint8 i = getNumLinesBet() + 1; i <= 25; i++)
	{
		spin->addLine(i);
	}
	for ( uint8 i = 26; i <= 50; i++ )
	{
		spin->addLine(i);
	}
	
    /**
     * Go through the played paylines one at a time looking for winners.
     */
    for ( uint8 lineidx = 0; lineidx < getNumLinesBet(); lineidx++ )
    {
		SpinLine *spinLine = spin->getSpinLineAt(lineidx);
		
        /**
         * Iterate the stops to get the classify positions.
         */
        for ( uint8 stopid = 0; stopid < spin->getStopCount(); stopid++ )
        {
            uint8 offsetPosition = getPayline(lineidx+1, stopid)->wheelposition;
            /**
             * Calculate the offset so we are using the proper icon
             * for this payline.
             */
            classifyPositions[stopid] = relativeWheelPosition(getGameModeId(), stopid, spin->getStops()[stopid], offsetPosition);
        }

        // Left to Right
		bool8 bWildSubstitution = false;
		int8 iMultiplier = 1;
		classifyZombiesVsCheerleadStops(classifyPositions, false, result, bWildSubstitution);

        int32 nPayout = getPayout(result)->payoutAmount;
		iMultiplier = bWildSubstitution ? WILD_SUBSTITUTION : 1;
		nPayout = nPayout * getBetPerLine();
        setTotalWin( getTotalWin() + (nPayout*iMultiplier));

		spinLine->basepayoutamount = nPayout;
		spinLine->totalmultiplier =  iMultiplier;
		spinLine->totallinepayoutamount = nPayout*iMultiplier;
		spinLine->jackpot = false;
		spinLine->payouttypeid = result;
		if (bWildSubstitution)
		{
			spinLine->addNote( NOTE_WILD_SUBSTITUTION );
		}

		// Right to Left
		bWildSubstitution = false;
		iMultiplier = 1;
		spinLine = spin->getSpinLineAt(lineidx + 25);
		classifyZombiesVsCheerleadStops(classifyPositions, true, result, bWildSubstitution);
		nPayout = getPayout(result)->payoutAmount;
		iMultiplier = bWildSubstitution ? WILD_SUBSTITUTION : 1;
		nPayout = nPayout * getBetPerLine();
        setTotalWin( getTotalWin() + (nPayout*iMultiplier));

		spinLine->basepayoutamount = nPayout;
		spinLine->totalmultiplier =  iMultiplier;
		spinLine->totallinepayoutamount = nPayout*iMultiplier;
		spinLine->jackpot = false;
		spinLine->payouttypeid = result;
		// Right to Left note
		if (nPayout > 0)
		{
			spinLine->addNote(NOTE_RIGHT_TO_LEFT);
		}
		if (bWildSubstitution)
		{
			spinLine->addNote( NOTE_WILD_SUBSTITUTION );
		}
	}

	// Did we get free spins?
	//  If three or more scatters, activate (or increment) free spins
	if (scatterResult == EVT_THREE_SCATTER ||  scatterResult == EVT_FOUR_SCATTER || scatterResult == EVT_FIVE_SCATTER)
	{
		incrementFreeSpins( 6 );
		setGameModeId( MODE_FREE_SPIN );
	}

    // Generate the wheel view for reporting
    storeWheelView(spin);

    delete [] classifyPositions;
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::classifyZombiesVsCheerleadStops()   
///
/// @param   stops      The array of stop positions for the line.
/// @param   bet        The value of the line bet.
/// @param   line       The index of the line.
///
/// This helper method is used to calculate the payout on a line.
////////////////////////////////////////////////////////////////////////////
void ZombiesVsCheerleadGuts::classifyZombiesVsCheerleadStops( uint8* stops, bool8 RightToLeft, uint8& rtnresult, bool8& wildSubstitution )
{
	// Calculate wild result. In Zombies Vs Cheerleaders game, this only makes sense in free spin mode
	SpinResult srWildLineWinResult = EVT_LOSER;
	if (getGameModeId() == MODE_FREE_SPIN)
	{
		srWildLineWinResult = getWildsWinResult( stops, RightToLeft );  
	}

	// Start with the icon on the first (or last) reel.
	uint8 iReel = RightToLeft ? 4 : 0;
	uint8 testIcon = getIcon( getGameModeId(), iReel, stops[iReel] )->iconid;
	// Wild substitution?
	if (getGameModeId() == MODE_FREE_SPIN)
	{
		if ((testIcon == ICON_PIC1 || testIcon == ICON_PIC2) && (iReel==0))
		{
			wildSubstitution = true;
			testIcon = ICON_WILD;
		}
		else if ((testIcon == ICON_PIC3 || testIcon == ICON_PIC4) && (iReel==4))
		{
			wildSubstitution = true;
			testIcon = ICON_WILD;
		}
	}

	// See what the spin result is if we used any Wild
	SpinResult srResultWithPossibleSubstitution =
		getLineWinResultWithPossibleSubstitution( stops, testIcon, RightToLeft, wildSubstitution );

	// Use the largest payout 
    if ( doesWildLineWinPayMore( srWildLineWinResult, srResultWithPossibleSubstitution, wildSubstitution ) )
	{
		// The WILDS by themselves pay out more so use them.
		rtnresult = srWildLineWinResult;
		wildSubstitution = false;
	}
	else
	{
		// Otherwise the result with the substitution (if any) paid more so use it.
		rtnresult = srResultWithPossibleSubstitution;
	}
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::SpinResult ZombiesVsCheerleadGuts::getWildsWinResult( uint8* stops, uint8 line, int32 addedWilds ) const
///
/// @param   stops			The array of stop positions for the line.
///
/// @return	 SpinResult		A payout from the SpinResult enumeration.
///
/// This helper method is used to calculate the Cauldron line win payout.
////////////////////////////////////////////////////////////////////////////
ZombiesVsCheerleadGuts::SpinResult ZombiesVsCheerleadGuts::getWildsWinResult( uint8* stops, bool8 RightToLeft )
{
	// Count how many Cauldrons we have at the beginning of the line.
	// Loop through the remaining reels until the streak is over
	uint8 iNumIcons = 0;
	int8 iReelStart = 0;
	int8 iReelEnd = 5;
	int8 iIncrement = 1;
	if (RightToLeft)
	{
		iReelStart = 4;
		iReelEnd = -1;
		iIncrement = -1;
	}
	for ( int8 i = iReelStart; i != iReelEnd; i = i+iIncrement, iNumIcons++ )
    {
		// If this icon is not a WILD
		int32 iIconID = getIcon( getGameModeId(), i, stops[i] )->iconid;
		if ( iIconID != ICON_WILD )
		{
			// Wild substitution?
			if (!((iIconID == ICON_PIC1 || iIconID == ICON_PIC2) && (i==0 || i==1)) &&
				!((iIconID == ICON_PIC3 || iIconID == ICON_PIC4) && (i==3 || i==4)))
			{
				break;
			}
		}
    }

	// Use the array to look up the spin result for iNumIcons Wilds.
	return lookupLineWinSpinResult( ICON_WILD, iNumIcons );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 ZombiesVsCheerleadGuts::SpinResult ZombiesVsCheerleadGuts::getLineWinResultWithPossibleSubstitution( uint8* stops, uint8 testIcon, int32 addedWilds ) const
///
/// @param   stops			The array of stop positions for the line.
/// @param   testIcon		The icon we are looking for, typically the first icon on the line.
/// @param   bSubstitution	An output parameter that is set if a subsitution is used.
///
/// @return	 SpinResult		A payout from the SpinResult enumeration.
///
/// This helper method is used to calculate the line win payout with a possible substitution.
////////////////////////////////////////////////////////////////////////////
ZombiesVsCheerleadGuts::SpinResult ZombiesVsCheerleadGuts::getLineWinResultWithPossibleSubstitution(
	uint8* stops, uint8 testIcon, bool8 RightToLeft, bool8& wildSubstitution )
{
	// Loop through the remaining reels until the streak is over
	uint8 iNumIcons = 1;
	int8 iReelStart = 1;
	int8 iReelEnd = 5;
	int8 iIncrement = 1;
	if (RightToLeft)
	{
		iReelStart = 3;
		iReelEnd = -1;
		iIncrement = -1;
	}
	for ( int8 i = iReelStart; i != iReelEnd; i = i+iIncrement, iNumIcons++ )
    {
		// See what this icon is.
		uint8 currentIcon = getIcon( getGameModeId(), i, stops[i] )->iconid;

		// If the current icon is a Cauldron (Wild)...
		if ( currentIcon == ICON_WILD )
		{
			// then use it as a substitution symbol and continue on.
			wildSubstitution = true;
			continue;
		}
		// If the current icon is substitued by a WILD
		else
		{			
			if (getGameModeId() == MODE_FREE_SPIN)
			{
				if ((currentIcon == ICON_PIC1 || currentIcon == ICON_PIC2) && (i==0 || i==1))
				{
					// PIC1 and PIC2 becomes WILD in first two reels
					wildSubstitution = true;
					continue;
				}
				else if ((currentIcon == ICON_PIC3 || currentIcon == ICON_PIC4) && (i==3 || i==4))
				{					
					// PIC3 and PIC4 becomes WILD in last two reels
					wildSubstitution = true;
					continue;
				}
			}
		}
		
		// Otherwise if the test icon is a WILD
		if ( testIcon == ICON_WILD )
		{
			// We have now found our first icon that is not a Wild.  All
			// of the rest of the icons will have to match this one now for the
			// streak to continue, so change our test icon.
			testIcon = currentIcon;
			continue;
		}

		// If the current icon is not the test icon...
        if ( currentIcon != testIcon )
        {
			break;											
        }
    }

	if (testIcon == ICON_WILD)
	{
		// We are not looking for wilds here. So this is a PIC1, PIC2, PIC3 or PIC4 (which are the ones becoming wild)
		// We choose the one which pays more (PIC1 or PIC2, first two reels)
		uint8 auxIcon1 = getIcon( getGameModeId(), 0, stops[0] )->iconid;
		uint8 auxIcon2 = getIcon( getGameModeId(), 1, stops[1] )->iconid;
		if (auxIcon1 == ICON_PIC1 || auxIcon2 == ICON_PIC1)
			testIcon = ICON_PIC1;
		else
			testIcon = ICON_PIC2;
	}

	// Use the array to look up the spin result for iNumIcons testIcons.
  	return lookupLineWinSpinResult( testIcon, iNumIcons );
}

////////////////////////////////////////////////////////////////////////////
/// @fn 	 bool ZombiesVsCheerleadGuts::doesWildLineWinPayMore( SpinResult srWilds, SpinResult srOther ) const
///
/// @param   srWilds		The spin result to calculate the Wilds payout for.
/// @param   srOther		The spin result to calculate the other payout for.
///
/// @return	 bool			True if the Cauldron win is larger.
///
/// This helper method is used to see which payout is larger.
////////////////////////////////////////////////////////////////////////////
bool ZombiesVsCheerleadGuts::doesWildLineWinPayMore( SpinResult srWilds, SpinResult srOther, bool8 bSubstitution ) const
{
	// Lookup the payout for the Cauldron line win.
	long nWildsLineWinPayout = getPayout( srWilds )->payoutAmount;

	// Lookup the payout for the other line win.
	uint8 iMultiplier = bSubstitution ? WILD_SUBSTITUTION : 1;
	long nOtherLineWinPayout = getPayout( srOther )->payoutAmount * iMultiplier;

	// Return true if the WILD payout is equal or bigger.
    return ( nWildsLineWinPayout >= nOtherLineWinPayout );
}

////////////////////////////////////////////////////////////////////////////
/// @fn		ZombiesVsCheerleadGuts::tryForJackpot()
///
///	This function tries to win the jackpot and adds the scatter if we hit
/// the jackpot.
////////////////////////////////////////////////////////////////////////////
void ZombiesVsCheerleadGuts::tryForJackpot( SlotSpin* spin )
{
	// If we are in the first paid spin (NOT a bonus spin)...
	if ( getGameModeId() == MODE_NORMAL ) 
	{
		// then try for the Random Progressive Jackpot
		// If we hit the jackpot...
		if ( getRandomProgressiveJackpotResult( getNumLinesBet() ) || m_bForceRandomJackpot)
		{
			// then add a jackpot scatter to the spin.
			spin->addScatter( 0,
				getPayout( EVT_RANDOM_PROGRESSIVE_WINNER )->payoutAmount, 1,
				true, EVT_RANDOM_PROGRESSIVE_WINNER, 0 );
		}
    }

	// In third party games (API) we include the jackpot in every spin so that they can update it.
	// If no jackpot, they have not won the jackpot
	setJackpotAmount( getProgressiveValue() );
}


void ZombiesVsCheerleadGuts::initDebugFunctionMap()
{
	SlotGuts::initDebugFunctionMap();	
}

